package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.BrandAddNewParam;
import cn.tedu.csmall.product.pojo.param.BrandUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/brand")
@Api(tags = "02.品牌管理模块")
@Slf4j
@Validated
public class BrandController {

    @Autowired
    private IBrandService brandService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/brand/add-new')")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(BrandAddNewParam brandAddNewParam){
        log.debug("开始处理【添加品牌】的请求,参数：{}", brandAddNewParam);
        brandService.addNew(brandAddNewParam);
        return JsonResult.success();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/pms/brand/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "BrandId" , value = "品牌ID", required = true , dataType = "long"),
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除品牌失败，请提交合法的ID值！")
                             @RequestParam Long id){
        log.debug("开始处理[根据ID删除品牌]的请求，参数:{}",id);
        brandService.delete(id);
        return JsonResult.deleteSuccess();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/pms/brand/update')")
    @ApiOperation("修改品牌")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@RequestParam @Range(min = 1, message = "请提交有效的品牌ID值！") Long id,
                             @Valid BrandUpdateInfoParam brandUpdateInfoParam){
        log.debug("开始处理[修改品牌详情]的业务,参数:{}",brandUpdateInfoParam);

        brandService.updateById(id,brandUpdateInfoParam);
        return JsonResult.updatesuccess();
    }


    @GetMapping("")
    @PreAuthorize("hasAuthority('/pms/brand/read')")
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",paramType = "query"),
            @ApiImplicitParam(name = "queryType",value = "查询类型",paramType = "all")
    }
    )
    public JsonResult list(@Range(min = 1,message = "查询品牌列表失败，请提供正确的页码值！") Integer page,
                           String queryType){
        log.debug("开始处理[查询品牌列表]的请求，页码：{}",page);

        //如果page为空，表示要查询所有的品牌，不为空则查询页
        Integer pageNum = page == null ? 1 :page;
        PageData<BrandListItemVO> pageData;

        //如果查询的为所有，则每页显示的为integer的最大值，则为显示所有
        if ("all".equals(queryType)){
            pageData = brandService.list(pageNum,Integer.MAX_VALUE);
        }
        else    {
            pageData = brandService.list(pageNum);
        }

        return JsonResult.success(pageData);
    }



    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/pms/brand/read')")
    @ApiOperation("根据ID查询品牌详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1,message = "根据ID查询品牌详情失败，请提交合法的ID值！")
                                      @RequestParam Long id){
        log.debug("开始处理[根据ID查询品牌详情]的请求，参数:{}",id);
        BrandStandardVO result = brandService.getStandardById(id);

        return JsonResult.success(result);
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/pms/brand/update')")
    @ApiOperation("启用品牌")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "请提交有效的品牌ID值！") Long id) {
        log.debug("开始处理【启用品牌】的请求，参数：{}", id);
        brandService.setEnable(id);
        return JsonResult.success();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/pms/brand/update')")
    @ApiOperation("禁用品牌")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交有效的品牌ID值！") Long id) {
        log.debug("开始处理【禁用品牌】的请求，参数：{}", id);
        brandService.setDisable(id);
        return JsonResult.success();
    }


}
